declare let eManager;
import DrawBase from "./DrawBase";
const {ccclass, property} = cc._decorator;

//1-2 抹面膜
@ccclass
export default class MianMo extends DrawBase {
    @property
    stepName = "";
    //面膜盒 
    @property(cc.Animation)
    MianMoHe: cc.Animation = null;

    //撕面膜Tips spine
    @property(sp.Skeleton)
    siMianMoTips: sp.Skeleton = null;

    //撕面膜spine
    @property(sp.Skeleton)
    siMianMoSk: sp.Skeleton = null;

    //麻子1
    @property(cc.Node)
    maziNode1: cc.Node = null;

    //麻子2
    @property(cc.Node)
    maziNode2: cc.Node = null;

    //痘痘
    @property(cc.Node)
    doudou: cc.Node[] = [];

    //音效
    @property({type:cc.AudioClip})
    moMiamMo: cc.AudioClip = null;

    _isSiMainMo = false;//撕面膜状态
    //撕面膜的距离判定
    _startPosx = 0;
    _audioId = 0;
    _isPlay = false;
    _isDone = false;
    initData(){
        //@ts-ignore
        this.MianMoHe.on("finished",this.startFinish,this);
        //@ts-ignore
        this.graphics = this.mask._graphics;  
        this.reset();
        this.generateKeyPoints();
        this._audioId = 0;
        eManager.emit("REFRESH_STEP_NAME", this.stepName);
        eManager.emit("HZ_REFRESH_ICON_TIPS", "step_1_2");
        this._isPlay = true;
        this.MianMoHe.play();
    };

    _touchStart(event){
        if(this._isPlay)
            return;
        if(this._isDone)
            return;
        if(this._isSiMainMo){
            this._startPosx = event.touch.getLocation();
        }else{
            if (this._audioId > 0) {
                cc.audioEngine.stop(this._audioId);
            }
            let worldPoint = event.touch.getLocation();
            let localPoint = this.MianMoHe.node.parent.convertToNodeSpaceAR(worldPoint);
            this.MianMoHe.node.x = localPoint.x -150;
            this.MianMoHe.node.y = localPoint.y +150;
            this._audioId =  cc.audioEngine.play(this.moMiamMo, true, 2);
            this._lastLoc = cc.v2(this.MianMoHe.node.x, this.MianMoHe.node.y);
        }
    };

    _touchMove(event){
        if(this._isPlay)
            return;
        if(this._isDone)
            return;
        if(!this._isSiMainMo){
            // let delta = event.touch.getDelta();
            // this.MianMoHe.node.x += delta.x;
            // this.MianMoHe.node.y += delta.y;
            let worldPoint = event.touch.getLocation();
            let localPoint = this.MianMoHe.node.parent.convertToNodeSpaceAR(worldPoint);
            this.MianMoHe.node.x = localPoint.x -150;
            this.MianMoHe.node.y = localPoint.y +150;
            this.drawLine(cc.v2(this.MianMoHe.node.x, this.MianMoHe.node.y));
        }
    };

    _touchEnd(event){
        cc.audioEngine.stop(this._audioId);
        this._audioId = 0;
        if(this._isPlay)
            return;
        if(this._isDone)
            return;
        if(this._isSiMainMo){
            let endPosx = event.touch.getLocation();
            //@ts-ignore
            if((endPosx.x - this._startPosx.x) > 50){
                this.playSiMianMo();
            }
            return;
        }
        let done = this.checkKeyPoints();
        //抹完面膜
        if (done) {
            //播放提醒撕面膜动画
            this.playSiMianMoTips();
        }
    };


    
    playSiMianMoTips(){
        this.mask.node.active = false;
        this.siMianMoSk.node.active = true;
        this.siMianMoTips.node.active = true;
        this.siMianMoSk.setAnimation(0,"moren",false);
        this.MianMoHe.node.active = false;
        this._isSiMainMo = true;
        eManager.emit("SET_VIBRATE", 500);
        this.maziNode1.active = false;
        this.maziNode2.active = true;
        for (let index = 0; index < this.doudou.length; index++) {
            this.doudou[index].active = false;
        }
    };

    playSiMianMo(){
        this._isDone = true;
        this.siMianMoTips.node.active = false;
        this.siMianMoSk.setAnimation(0,"mianmo",false);
        this.siMianMoSk.setCompleteListener(()=>{
            this.node.active = false;
            eManager.emit("DONE_STEP", 1);
            eManager.emit("MINI_DONE_POINT", 2);
            eManager.emit("HZ_MAKE_DOT", {
                makeup_close:1,
                makeup_step:this.stepName
            });
        });
    };

    startFinish(){
        this._isPlay = false;
    };
}
